package com.pb.controller;

import com.pb.pojo.Score;
import com.pb.pojo.Subject;
import com.pb.pojo.PageResult;
import com.pb.pojo.Result;
import com.pb.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/scores")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @PostMapping
    public Result save(@RequestBody Score score){
        scoreService.save(score);
        return Result.success();
    }

    @GetMapping
    public Result page(String studentName ,
                       String moduleName,
                       @RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10")Integer pageSize){
        PageResult pageResult = scoreService.page(studentName,moduleName, page , pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        Score score = scoreService.getInfo(id);
        return Result.success(score);
    }


    @PutMapping
    public Result update(@RequestBody Score score){
        scoreService.update(score);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        scoreService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Score> scoretList = scoreService.findAll();
        return Result.success(scoretList);
    }
}
